//
//  PurchaseHistoryVC.swift
//  LeTu
//
//  Created by gy on 2023/12/8.
//

import UIKit

class PurchaseHistoryVC: BaseViewController {

    private var viewModel = PurchaseHistoryVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
        historyTableView.headerBeginRefreshing()

    }
    
    // MARK: - getData
    func getData(isLoad: Bool) {
        
        if isLoad == false {
            historyTableView.footerResetNoMoreData()
        }
        
        viewModel.getCollects(isLoad: isLoad) { [weak self] dialog in
            
            guard let strongSelf = self else { return }
            
            DispatchQueue.main.async {
                
                if isLoad {
                    strongSelf.historyTableView.footerEndRefreshing()
                } else {
                    strongSelf.historyTableView.headerEndRefreshing()
                }
                
                if dialog.code != 200 {
                    strongSelf.showMessage(message: dialog.message)
                }
                
                if strongSelf.viewModel.sourceArr.count == 0 || strongSelf.viewModel.sourceArr.count % strongSelf.viewModel.pageSize > 0 {
                    strongSelf.historyTableView.footerEndRefreshingWithNoMoreData()
                }
                strongSelf.historyTableView.reloadData()
                
            }
            
        }
        
    }
    
    // MARK: - configUI
    func configUI() {
        
        navIsHidden = false
        title = "购买记录".localized
        
        view.backgroundColor = .app_viewBackgroundColor
        view.addSubview(historyTableView)
        
        historyTableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(10)
        }
        
        historyTableView.delegate = self
        historyTableView.dataSource = self
        historyTableView.register(cellClass: BaseEmptyTableViewCell.self)
        historyTableView.register(cellClass: PurchaseHistoryTableViewCell.self)
        historyTableView.addRefreshHeaderCallback { [weak self] in
            guard let strongSelf = self else { return }
            strongSelf.getData(isLoad: false)
        }
        historyTableView.addRefreshFooterCallback { [weak self] in
            guard let strongSelf = self else { return }
            strongSelf.getData(isLoad: true)
        }
        
    }
    private lazy var historyTableView: UITableView = {
        let historyTableView = UITableView()
        historyTableView.tableFooterView = UIView()
        historyTableView.showsVerticalScrollIndicator = false
        return historyTableView
    }()
    
}

extension PurchaseHistoryVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return viewModel.sourceArr.count == 0 ? tableView.height:PurchaseHistoryTableViewCell.getCellHeight()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.sourceArr.count == 0 ? 1:viewModel.sourceArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if viewModel.sourceArr.count == 0 {
            let cell = tableView.dequeueReusableCell(BaseEmptyTableViewCell.self, indexPath: indexPath)
            cell.setInfo(iconName: "nodata", title: "暂无数据".localized)
            return cell
        }
        let cell = tableView.dequeueReusableCell(PurchaseHistoryTableViewCell.self, indexPath: indexPath)
        cell.setHistoryModel(viewModel.sourceArr[indexPath.row])
        return cell
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard viewModel.sourceArr.count > 0 else { return }

    }
    
}

